
<style type="text/css">
	html,body{height: 100%;overflow: hidden;user-select:none;-webkit-touch-callout:none;-webkit-user-select:none;}
	.bugGoodsBg{text-align: center;max-height: 458px;min-height: 300px;padding-top: 10%;}
	.bugGoodsText{text-align: center;font-size: 16px; line-height: 24px;color: #555;position: fixed;bottom: 85px;left: 0;text-align: center;width: 100%;}
	.bugGoodsText strong{color: #FFA000;font-size: 24px;}
	.voice{
		height: 140px;
	    width: 140px !important;
	    margin: 0 auto;
	    display: block;
	    border-radius: 70px;
	    background-color: #f49e10 !important;
	    color: #fff !important;
	    padding: 0 5px;
    	line-height: 140px;
	}
	.hovered{
		background-color: #985F02 !important
	}
	.bugGoodsBg img{display: block;max-width: 495px;width: 100%;}
	.bar-header input{display: block;}
	.bar-header .text{width: 32px;height: 32px;border: 1px solid #ffa000;border-radius: 100%;-webkit-border-radius: 100%;margin: 0 0 0 10px;background:#fff url({MODULE_URL}public/images/keybord.png) no-repeat center center;background-size: 20px auto;}
	.bar-header .voice{-webkit-box-flex: 1;box-flex: 1;max-height: 32px;background: #ffa000;border-radius: 5px;-webkit-border-radius: 5px;margin:0 10px;text-align: center;line-height:32px;color: #fff;}
</style>
<div class="bar bar-header bar-clear">
	<a href="" class="button button-clear"></a>
	<button id="buy_text" class="button button-clear" data-pjax href="">
		<span class="active">手工输入</span>
		<i class="icon ion-ios-arrow-right active"></i>
	</button>
	
</div>
<div class="wrapper has-header has-footer" style="margin-top:4px;margin-bottom:44px;">
    <div class="list">
        <div class="bugGoodsBg">
            <img src="{MODULE_URL}public/images/bug.png" width="100%" alt=""/>
        </div>
    </div>
    <div class="list">
    	<button ontouchstart = "return false;" class="button button-outline voice light" class="-webkit-user-select: none;-webkit-touch-callout: none;" id="voice">想买长按我！</button>
    </div>
    <div style="height:44px;"></div>
</div>

<input type="hidden" name="province"/>
<input type="hidden" name="city"/>
<script>
require(['jquery','weixin','core','map','js/tool','js/template','js/index','js/convertor'],function($,wx,core,BMap,R){
	$('#footer').show();
	
	wx.config(jssdkconfig);
	
	core.post('check',{act:'post'},function(data){
		if(data.status != 0){
			core.ok(""+data.message,function(){
				$.pjax({
					url:"" + data.url,
					container:'#pjax-container'
				});
			},function(){
				$.pjax({
					url:"" + data.url,
					container:'#pjax-container'
				});
			});
		}
	});
	
	$('#buy_text').click(function(){
		var province = $('input[name="province"]').val();
		var city = $('input[name="city"]').val();
		if(!province || !city){
			core.ok("定位失败",function(){
				
			});
		}else{
			$.pjax({
				url:"{php echo $this->createMobileUrl('buy_text')}&province="+province+"&city="+city,
				container:"#pjax-container"
			});
		}
	});
	
	var timemax = 60;
    //微信最大录音时间是60s
    var curtime = 0;
    //当前已录音时长
    var timer = null ;
    
    /**
     * 快速点击有时候会先触发结束录音后触发开始录音
     * 0: 初始态 1:开始录音 2:结束录音
     */
      var voicestate = 0;
      function setLocalId(res) {
          //隐藏提示层
          R.hideVoice();
          if (curtime < 3) {
              core.cancel('语音不可少于3秒，请重新录制',function(){});
              return;
          }
          //获取当前用户所在省市
          var province = $('input[name="province"]').val(), 
          city = $('input[name="city"]').val();
          if (res && res.localId) {
        	  window.location.href = "{php echo $this->createMobileUrl('buy_voice')}&province="+province+"&city="+city+"&voiceid="+res.localId+"&time="+curtime;
          } else {
              core.cancel('录音异常请重新录制');
          }
      }
      
      function end() {
          //停止录音
          stop();
          wx.stopRecord({
        	  success: function(res) {
                  if (voicestate == 1) {
                      voicestate = 0;
                      setLocalId(res);
                  } else {
                      voicestate = 2;
                  }
              }
          });
      }
      
      function stop() {
          //停止录音计时
          if (timer) {
              clearInterval(timer);
          }
      }
      
      function start() {
          //启动录音计时
          stop();
          curtime = 0;
          timer = setInterval(function() {
              curtime++;
              if (curtime == timemax) {
                  end();
              }
          }, 1000);
      }
      //录音功能
      
      function voice() {
          wx.ready(function() {
              wx.onVoiceRecordEnd({
                  complete: function(res) {
                	  voicestate = 0;
                	  stop();
                      setLocalId(res);
                  }
              });
              
          });
      }
      wx.ready(function() {
    	  $('#voice')[0].addEventListener('touchstart',function(e) {
        	  console.log('start');
        	  R.showVoice();
        	  $('.voice').addClass('hovered');
        	  setTimeout(function(){
        		  wx.onVoiceRecordEnd({
                      complete: function(res) {
                    	  if(voicestate == 1){
                    		  voicestate = 0;
                        	  stop();
                              setLocalId(res);
                    	  }
                      }
                  });
    			  wx.startRecord({
                      success: function() {
                          if (voicestate == 0) {
                              voicestate = 1;
                              curtime = 0;
                              start();
                          } else {
                              voicestate = 0;
                              wx.stopRecord();
                          }
                      }
                  });
        		  
        	  },0);
          },false);
          
          $('#voice')[0].addEventListener('touchend', function(e) {
        	  console.log('end');
        	  R.hideVoice();
              $('.voice').removeClass('hovered');
        	  end();
          },false);
	  });
	
	function location() {
        $.ajax({
            url: 'http://api.map.baidu.com/location/ip?ak=F51571495f717ff1194de02366bb8da9',
            dataType: "jsonp",
            jsonp: "callback",
            timeout: 5000,
            success: function(data) {
                $('input[name="province"]').val(data.content.address_detail.province);
                $('input[name="city"]').val(data.content.address_detail.city);
                console.log(data.content.address_detail.province+data.content.address_detail.city);
            },
            error: function(msg) {
                alert(msg);
            }
        });
    }
	
	
	//微信获取地理经纬度
    function wxGetLocation() {
        wx.ready(function() {
            wx.getLocation({
                success: function(res) {
                    var lat = res.latitude;
                    // 纬度，浮点数，范围为90 ~ -90
                    var lng = res.longitude;
                    // 经度，浮点数，范围为180 ~ -180。
                    var position = {
                        'coords': {
                            'longitude': lng,
                            'latitude': lat
                        }
                    };
                    showPosition(position);
                },
                fail: function() {
                    alert('请在系统设置里打开微信定位功能');
                }
            });
        });
    }
    
    //根据经纬度转换成物理地址
    function showPosition(position) {
        var gpsPoint = new BMap.Point(position.coords.longitude,position.coords.latitude);
        BMap.Convertor.translate(gpsPoint, 0, function(point) {
        	var geoc = new BMap.Geocoder();
	        var pt = new BMap.Point(point.lng,point.lat); 
	        geoc.getLocation(pt, function(rs){
	        	 var addComp = rs.addressComponents;
	        	 $('input[name="province"]').val(addComp.province);
		         $('input[name="city"]').val(addComp.city);
		         console.log(addComp.province+addComp.city);
	        });
        });
    }
    
    function getLocation(){
	    if(core.isWeiXin()){
	    	wxGetLocation();
	    }else{
	    	location();
	    }
	}
    
    getLocation();
    
});
</script>
{template 'default/common/share'}